package org.demo.action;

import java.util.List;

import javax.servlet.ServletRequest;

import org.apache.struts2.ServletActionContext;
import org.demo.bean.Person;
import org.demo.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 控制层：PersonAction
 * @author  
 * @date    2010-7-21
 * @file    org.demo.action.PersonAction.java
 */
public class PersonAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
	private PersonService personService;
	/**
	 * viewbean
	 */
	private Person person;
	
	/**
	 * 转到增加页面
	 * @return
	 */
	public String addUI(){
		return "addUI";
	}
	/**
	 * 保存
	 * @return
	 */
	public String save(){
		// 保存数据
		personService.save(person);
		// new 一个新的 person
		person = new Person();
		// 返回到查询页
		return list();
	}
	/**
	 * 查询所有
	 * @return
	 */
	public String list(){
		// 查询数据
		List<Person> list = personService.getAll();
		// 将数据放到 request 中
		ServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("personList", list);
		// 返回
		return "list";
	}
	
	/**
	 * get person
	 * @return
	 */
	public Person getPerson() {
		return person;
	}
	/**
	 * set person
	 * @param person
	 */
	public void setPerson(Person person) {
		this.person = person;
	}
}